#!/usr/bin/env python
# -*- coding: utf-8 -*-
from werkzeug.security import generate_password_hash, check_password_hash
from flask_wtf import FlaskForm
from wtforms import TextAreaField, PasswordField
from wtforms.validators import InputRequired, EqualTo
from my_app import db


class User(db.Model):
    """创建数据库用户模型"""
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100))
    pwdhash = db.Column(db.String())

    def __init__(self, username, password):
        self.username = username
        self.pwdhash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.pwdhash, password)

    @property
    def is_authenticated(self):
        """返回true 仅当用户不需要进行身份验证时才返回false"""
        return True

    @property
    def is_active(self):
        """返回true 仅当阻止或禁用了某个用户时才会返回false"""
        return True

    @property
    def is_anonymous(self):
        """用于指示不应登录到系统并应以匿名方式访问应用程序的用户。对于正常的用户应返回False"""
        return False

    def get_id(self):
        """用于标识用户的唯一ID， 且应该是一个Unicode"""
        return str(self.id)


class RegistrationForm(FlaskForm):
    """用户注册表单"""
    username = TextAreaField('用户名', [InputRequired()])
    password = PasswordField('密码', [InputRequired(), EqualTo('confirm', message='两次输入的密码必须保持一致')])
    confirm = PasswordField('确认密码', [InputRequired()])


class LoginForm(FlaskForm):
    """用户登录表单"""
    username = TextAreaField('用户名', [InputRequired()])
    password = PasswordField('密码', [InputRequired()])



